/*
 www.pipoisu.com
 Copyright (c) 2011.  All rights reserved.

 File     : pipo.h
 Author(s): Erez Raviv

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#pragma once



#define DWT_CYCCNT  ((volatile uint32_t *)0xE0001004)


// for roundf()
#define __USE_C99_MATH

#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>

#include "stm32f10x_conf.h"
#include "core_cm3.h"

#ifndef M_PI
#define M_PI       3.14159265358979323846
#endif /* M_PI */


#define digitalHi(p, i)     { p->BSRR = i; }
#define digitalLo(p, i)     { p->BRR = i; }
#define digitalToggle(p, i) { p->ODR ^= i; }

// Hardware definitions and GPIO

#define LED_RED_GPIO     GPIOC
#define LED_RED_PIN      GPIO_Pin_0
#define LED_GREEN_GPIO   GPIOC
#define LED_GREEN_PIN    GPIO_Pin_1


// Helpful macros
#define LED_RED_FLIP     digitalToggle(LED_RED_GPIO, LED_RED_PIN);
#define LED_RED_OFF      digitalHi(LED_RED_GPIO, LED_RED_PIN);
#define LED_RED_ON       digitalLo(LED_RED_GPIO, LED_RED_PIN);

#define LED_GREEN_FLIP   digitalToggle(LED_GREEN_GPIO, LED_GREEN_PIN);
#define LED_GREEN_OFF    digitalHi(LED_GREEN_GPIO, LED_GREEN_PIN);
#define LED_GREEN_ON     digitalLo(LED_GREEN_GPIO, LED_GREEN_PIN);

#define TICKS_PER_SEC 8000000
#define STARTUP_DELAY 500 //500msec

typedef void (* uartReceiveCallbackPtr)(uint16_t data);     // used by uart2 driver to return frames to app


/*************************************************************************
 Macros
*************************************************************************/
#define MIN(a,b)           ( ((a)<(b)) ? (a) : (b) )
#define MAX(a,b)           ( ((a)>(b)) ? (a) : (b) )
#define MINMAX(x,min,max)  ( MIN(MAX((x),(min)),(max)) )
#define INTMINMAX(x)       ( MINMAX(x,-0x7FFF,0x7FFF)  )
#define CONSTRAIN(x,a)     ( MINMAX(x,-(a),(a)) )
#define SWAP(a,b)          ( ((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)) )


extern int16_t attitudeCMD[3];
extern float deltaT;
extern uint8_t magnetometer;
extern volatile uint16_t g25msec;
extern volatile uint8_t flashRedLed;
extern uint16_t maxPWMPeriod;


void delayMicroseconds(uint32_t us);

#include "i2cmaster.h"
#include "uart.h"
#include "sensors.h"
#include "imu.h"
#include "eeprom.h"
#include "pwm.h"
#include "mixer.h"
#include "system.h"


